#!/usr/bin/env python

# -*- coding: utf-8 -*

"""
红外避障传感器，包含左右两侧
"""

from py_code.driver.Pin import Pin
from py_code.SingletonType import SingletonType
from py_code.driver.GpioDriver import GpioDriver
import RPi.GPIO as GPIO
import logging

class InfraredObstacleAvoidance(GpioDriver, metaclass = SingletonType):
    """红外避障传感器，一共有左右两侧"""
    pins = [Pin(21, GPIO.IN, description = "left"), Pin(19, GPIO.IN, description = "right"), Pin(22, GPIO.OUT, description = "开关")]

    def open(self):
        super().open()
        # 打开开关
        self.pins[2].writeHigh()
    
    def leftOk(self) -> bool:
        """左边是否是无障碍物"""
        v = self.pins[0].read()
        logging.debug(f"红外避障：左边{'没有' if v else '有'}障碍物")
        return v
    
    def rightOk(self) -> bool:
        """右边是否是无障碍物"""
        v = self.pins[1].read()
        logging.debug(f"红外避障：右边{'没有' if v else '有'}障碍物")
        return v

    def close(self):
        self.pins[2].writeLow()
        logging.debug(f"红外避障，关闭驱动")
        return super().close()
 